package com.lzx.applib.utils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.text.TextUtils;

public class StreamUtil {

	
	public static void closeSafely(Closeable...closeables) {
		if (closeables == null) return;
		for (Closeable closeable : closeables){
			try {
				closeable.close();
			} catch (Exception e) {}
		}
	}
	
	public static String streamToString(InputStream in) throws Exception {
		StringBuilder result = new StringBuilder();
		byte[] buf = new byte[1024*10];
		int length = -1;
		while ((length = in.read(buf))>0) {
			result.append(new String(buf, 0, length));
		}
		return result.toString();
	}
	
	public static String[] streamToStringArray(InputStream in) throws Exception {
		List<String> list = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line = reader.readLine();
		while (line != null) {
			if (!TextUtils.isEmpty(line)) 
				list.add(line);
			line = reader.readLine();
		}
		return list.toArray(new String[0]);
 	}
	
}
